﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Collections;

namespace AliboboStockLib.webutil
{
    public class HttpGet
    {
        protected static void prepareParameter(  HttpWebRequest webRequest ,Hashtable parameter)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("");
            int sz = parameter.Keys.Count;
            int i = 0;
            foreach(String k in  parameter.Keys)
            {
                i++;
                String v = (String)parameter[k];
                sb.Append(k);
                sb.Append("=");
                sb.Append(v);
                if(i<sz)
                sb.Append("&");
            }
            String data = sb.ToString();
            byte[] dataStream = Encoding.UTF8.GetBytes(data);
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.ContentLength = dataStream.Length;

            Stream newStream = webRequest.GetRequestStream();
            // Send the data.
            newStream.Write(dataStream, 0, dataStream.Length);
            newStream.Close();

        }
        public static string requestBig5(string url,Hashtable parameter=null,Hashtable headerset=null)
        {
            string responseStr = null;
            HttpWebResponse webResponse = null;
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            if (headerset != null)
            {
                webRequest.KeepAlive = true;
                foreach (String k in headerset.Keys)
                {
                    String v = (String)headerset[k];
                    if ("Referer".Equals(k))
                        webRequest.Referer = v;
                    if ("Host".Equals(k))
                        webRequest.Host = v;

                    if ("User-Agent".Equals(k))
                        webRequest.UserAgent = v;

                    if ("Host".Equals(k))
                        webRequest.Host = v;

                    if ("Accept".Equals(k))
                        webRequest.Accept = v;
                }
            }
            webRequest.Method = "POST";
            webRequest.Timeout = 50000;
            if (parameter != null)
            {
                prepareParameter(webRequest, parameter);
            }
            else
            {
                webRequest.ContentType = "text/html; charset=big5";
            }
         
            try
            {
                webResponse = (HttpWebResponse)webRequest.GetResponse();
            }
            catch (WebException e)
            {
                responseStr = "Error happen! please try later";
                System.Diagnostics.Debug.WriteLine(e.Message);
            }
            catch (Exception e)
            {
                //发生异常时把错误信息当作错误信息返回
                responseStr = "Error msg:" + e.Message;

            }
            finally
            {
                if (webResponse != null)
                {
                    //获得网络响应流
                    using (StreamReader responseReader = new StreamReader(webResponse.GetResponseStream(), Encoding.GetEncoding("BIG5")))
                    {
                        responseStr = responseReader.ReadToEnd();//获得返回流中的内容
                    }
                    webResponse.Close();//关闭web响应流
                }
            }
            return responseStr;
        }
    }
}
